package com.lucene;

import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.cn.smart.SmartChineseAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.FuzzyQuery;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.PrefixQuery;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.search.WildcardQuery;
import org.apache.lucene.store.FSDirectory;
import org.junit.Test;

public class Test_1 {

	/**
     * 通配符搜索（顾名思义）<br/><br/>
     * 
     * 主要对象是：WildcardQuery，调用方式如下：<br/><br/>
     * 
     * Term term=new Term(字段名,搜索关键字+通配符);<br/>
     * WildcardQuery wildcardquery=new WildcardQuery(term);<br/>
     * Hits hits=searcher.search(wildcardquery);<br/><br/>
     * 
     * 其中的通配符分两种，即*和？<br/>
     * * 表示任意多的自负<br/>
     * ？表示任意一个字符
     * @throws Exception
     */
	@Test
	public void wildcardQuery() throws Exception {
		IndexReader reader = DirectoryReader.open(FSDirectory.open(Paths.get("D:\\index")));
		IndexSearcher searcher = new IndexSearcher(reader);

		Term term = new Term("name", "c*");
		WildcardQuery wildcardQuery = new WildcardQuery(term);

		TopDocs topDocs = searcher.search(wildcardQuery, 1000);
		System.out.println("共检索出 " + topDocs.totalHits + " 条记录");
		System.out.println();

		ScoreDoc[] scoreDocs = topDocs.scoreDocs;
		for (ScoreDoc scDoc : scoreDocs) {
			Document document = searcher.doc(scDoc.doc);
			String body = document.get("body");
			String name = document.get("name");
			String modifiedtime = document.get("modifiField");
			// 日期格式转换
			long t = Long.parseLong(modifiedtime);
			String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(t));
			float score = scDoc.score; // 相似度
			System.out.println("文件名：" + name);
			System.out.println("日期：" + time);
			System.out.println("文件内容：" + body);
			System.out.println("相似度:" + score);

		}
		reader.close();
	}

	@Test
	public void fuzzyQuery() throws Exception {
		IndexReader reader = DirectoryReader.open(FSDirectory.open(Paths.get("D:\\index")));
		IndexSearcher searcher = new IndexSearcher(reader);

		Term term = new Term("name", "c");
		FuzzyQuery fuzzyquery = new FuzzyQuery(term);

		TopDocs topDocs = searcher.search(fuzzyquery, 1000);
		System.out.println("共检索出 " + topDocs.totalHits + " 条记录");
		System.out.println();

		ScoreDoc[] scoreDocs = topDocs.scoreDocs;
		for (ScoreDoc scDoc : scoreDocs) {
			Document document = searcher.doc(scDoc.doc);
			String body = document.get("body");
			String name = document.get("name");
			String modifiedtime = document.get("modifiField");
			// 日期格式转换
			long t = Long.parseLong(modifiedtime);
			String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(t));
			float score = scDoc.score; // 相似度
			System.out.println("文件名：" + name);
			System.out.println("日期：" + time);
			System.out.println("文件内容：" + body);
			System.out.println("相似度:" + score);
		}
		reader.close();
	}
	
	 @Test
	    public void prefixQuery() throws Exception {
	        IndexReader reader = DirectoryReader.open(FSDirectory.open(Paths.get("D:\\index")));
	        IndexSearcher searcher = new IndexSearcher(reader);
	        
	        Term term=new Term("name", "a"); 
	        PrefixQuery prefixquery=new PrefixQuery(term);
	        
	        TopDocs topDocs=searcher.search(prefixquery, 1000);
	        System.out.println("共检索出 " + topDocs.totalHits + " 条记录");
	        System.out.println();
	        
	        
	        ScoreDoc[] scoreDocs = topDocs.scoreDocs;
	        for (ScoreDoc scDoc : scoreDocs) {
	        	Document document = searcher.doc(scDoc.doc);
				String body = document.get("body");
				String name = document.get("name");
				String modifiedtime = document.get("modifiField");
				// 日期格式转换
				long t = Long.parseLong(modifiedtime);
				String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(t));
				float score = scDoc.score; // 相似度
				System.out.println("文件名：" + name);
				System.out.println("日期：" + time);
				System.out.println("文件内容：" + body);
				System.out.println("相似度:" + score);
	        }
	        reader.close();
	    }
	    
}
